<template>
  <el-form
    ref="registerForm"
    :model="form"
    :rules="rules"
    @submit.prevent="handleSubmit"
    class="register-form"
  >
    <el-form-item prop="username">
      <el-input
        v-model="form.username"
        placeholder="请输入用户名"
        size="large"
        :prefix-icon="User"
      />
    </el-form-item>
    
    <el-form-item prop="email">
      <el-input
        v-model="form.email"
        placeholder="请输入邮箱"
        size="large"
        :prefix-icon="Message"
      />
    </el-form-item>
    
    <el-form-item prop="password">
      <el-input
        v-model="form.password"
        type="password"
        placeholder="请输入密码"
        size="large"
        :prefix-icon="Lock"
        show-password
      />
    </el-form-item>
    
    <el-form-item prop="confirmPassword">
      <el-input
        v-model="form.confirmPassword"
        type="password"
        placeholder="请再次输入密码"
        size="large"
        :prefix-icon="Lock"
        show-password
      />
    </el-form-item>
    
    <el-form-item prop="role">
      <el-select 
        v-model="form.role" 
        placeholder="请选择角色" 
        size="large"
        class="role-select"
      >
        <el-option label="学生" value="student">
          <div class="role-option">
            <el-icon><User /></el-icon>
            <span>学生</span>
          </div>
        </el-option>
        <el-option label="教师" value="teacher">
          <div class="role-option">
            <el-icon><School /></el-icon>
            <span>教师</span>
          </div>
        </el-option>
        <el-option label="管理员" value="admin">
          <div class="role-option">
            <el-icon><Setting /></el-icon>
            <span>管理员</span>
          </div>
        </el-option>
      </el-select>
    </el-form-item>
    
    <el-form-item>
      <el-button
        type="primary"
        native-type="submit"
        :loading="loading"
        size="large"
        class="submit-btn"
      >
        <el-icon v-if="!loading"><Plus /></el-icon>
        {{ loading ? '注册中...' : '注册' }}
      </el-button>
    </el-form-item>
    
    <div class="form-agreement">
      <el-checkbox v-model="agreement">
        我已阅读并同意
        <el-link type="primary">用户协议</el-link>
        和
        <el-link type="primary">隐私政策</el-link>
      </el-checkbox>
    </div>
  </el-form>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Message, Lock, School, Setting, Plus } from '@element-plus/icons-vue'

export default {
  name: 'RegisterForm',
  components: {
    User,
    Message,
    Lock,
    School,
    Setting,
    Plus
  },
  setup() {
    const router = useRouter()
    const registerForm = ref()
    const loading = ref(false)
    const agreement = ref(false)
    
    const form = reactive({
      username: '',
      email: '',
      password: '',
      confirmPassword: '',
      role: 'student'
    })
    
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度至少 6 个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, validator: validateConfirmPassword, trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择角色', trigger: 'change' }
      ]
    }
    
    const handleSubmit = async () => {
      if (!registerForm.value) return
      
      if (!agreement.value) {
        ElMessage.warning('请先同意用户协议和隐私政策')
        return
      }
      
      try {
        await registerForm.value.validate()
        loading.value = true
        
        // 模拟注册API调用
        setTimeout(() => {
          ElMessage.success('注册成功，请登录')
          router.push('/login')
          loading.value = false
        }, 1000)
        
      } catch (error) {
        console.error('注册错误:', error)
        ElMessage.error('注册失败')
        loading.value = false
      }
    }
    
    return {
      registerForm,
      form,
      rules,
      loading,
      agreement,
      handleSubmit,
      User,
      Message,
      Lock,
      School,
      Setting,
      Plus
    }
  }
}
</script>

<style scoped>
.register-form {
  width: 100%;
}

.register-form .el-form-item {
  margin-bottom: 25px;
}

.submit-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 25px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.form-agreement {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #7f8c8d;
}

.role-select {
  width: 100%;
}

.role-option {
  display: flex;
  align-items: center;
  gap: 10px;
}

.role-option .el-icon {
  font-size: 16px;
}

.el-input {
  border-radius: 10px;
}

.el-input__wrapper {
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-input__wrapper:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.el-input__wrapper.is-focus {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.el-select {
  border-radius: 10px;
}

.el-select .el-input__wrapper {
  border-radius: 10px;
}
</style> 